#const
Inventory=~/.envs/ansible/hosts.yaml
ansiblePlay=ansible-playbook -v -e "varHosts=${H}" -e "proxyUrl=${hp}" $(roleHome)/play-book.yaml -i $(Inventory)
# mod
role=$(word 1,${MAKECMDGOALS})
roleCmd=$(word 2,${MAKECMDGOALS})
inArgs=$(filter-out toolHome=% repxHome=%, $(MAKEOVERRIDES))

#dirs
scriptsDir=${toolHome}/scripts
filesDir=${toolHome}/files
rolesDir=${toolHome}/roles
binsDir=${toolHome}/bin
pwdDir=${CURDIR}

roleHome=${rolesDir}/${role}
taskHome=${roleHome}/tasks

#${ansiblePlay} -e "icmd=uninstall" --tags uninstall
define wraptags =
-e "icmd=$(1)" --tags $(1),$(2)
endef
define chkNotNull =
${scriptsDir}/assertx.sh NotNull $(1) $(2)
endef



ifeq ($(findstring $(role),help setEnv addAll push commit newkey synckey lsh),$(role))

cmds:
	@$(scriptsDir)/helpx.sh cmds ${toolHome}

lsh: #列出当前环境所有机器列表
	@ansible all -i $(Inventory) --list-hosts

help: #帮助 打印模块信息
	@$(scriptsDir)/helpx.sh toolHelp ${toolHome}

setEnv: #设置环境文件 # F=? //fileName V=? //fileTextValue
	@${scriptsDir}/assertx.sh NotNull F ${F}
	@${scriptsDir}/assertx.sh NotNull V ${V}
	@if [ ! -d ~/.envs ]; then mkdir ~/.envs; fi;
	@echo "${V}">~/.envs/${F}
	@cat ~/.envs/${F}

addAll: #git add *
	cd ${toolHome} && git add -f *

commit: #git commit -m # M=? //info
	@${scriptsDir}/assertx.sh NotNull M ${M}
	cd ${toolHome} && git commit -m ${M}
	
push: #git push origin master
	cd ${toolHome} && git push origin master

newkey: #new sshkey
	@ssh-keygen -t rsa -b 2048 -P ''

synckey: #sync sshkey #U=? user , H=? //hostsname or ip
	@${scriptsDir}/assertx.sh NotNull "user" ${U}
	@${scriptsDir}/assertx.sh NotNull "host" ${H}
	@ssh-copy-id root@${H}
else 

mkfile=${roleHome}/Makefile-${role}.mk
#自动加载模块
include ${mkfile}

${role}:
ifneq (${roleCmd},) #如果存在角色命令则执行
	@${scriptsDir}/assertx.sh HostNotNull "${mkfile}" ${roleCmd} ${H}
	@chmod +x $(scriptsDir)/helpx.sh $(scriptsDir)/assertx.sh $(scriptsDir)/ensurex.sh
else # 否则显示帮助信息
	@$(scriptsDir)/helpx.sh modHelp ${mkfile} ${role} ${toolName}
endif	
endif

#cmdGoals=$(filter-out ${role} ${toolName}, $(MAKECMDGOALS))
#mkFiles=$(foreach n,$(cmdGoals),${roleHome}/$n.mk)
#include ${mkFiles}